package com.sky.controller.admin;

import com.sky.constant.ShopConstant;
import com.sky.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController("adminShopController") // admin端和user端的controller名称相同导致bean名字相同(都是类名开头小写)，需要通过注解重命名
@RequestMapping("/admin/shop")
@Slf4j
public class ShopController {
    @Autowired
    private RedisTemplate redisTemplate;

    @PutMapping("/{status}")
    public Result<String> setStatus(@PathVariable String status) {
        log.info("设置店铺状态: {}", Objects.equals(status, ShopConstant.ENABLE) ? "营业中" : "已打烊");
        redisTemplate.opsForValue().set(ShopConstant.KEY, status);
        return Result.success();
    }

    @GetMapping("status")
    public Result<Integer> getStatus() {
        String status = (String) redisTemplate.opsForValue().get(ShopConstant.KEY);
        log.info("获取店铺状态: {}", Objects.equals(status, ShopConstant.ENABLE) ? "营业中" : "已打烊");
        assert status != null;
        return Result.success(Integer.valueOf(status));
    }
}
